package com.example.roommessagetest;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

import im.zego.zegoexpress.ZegoExpressEngine;
import im.zego.zegoexpress.callback.IZegoEventHandler;
import im.zego.zegoexpress.callback.IZegoIMSendBroadcastMessageCallback;
import im.zego.zegoexpress.constants.ZegoScenario;
import im.zego.zegoexpress.entity.ZegoBroadcastMessageInfo;
import im.zego.zegoexpress.entity.ZegoRoomConfig;
import im.zego.zegoexpress.entity.ZegoUser;

public class RoomMessageActivity extends AppCompatActivity implements View.OnClickListener {
    Button send;
    TextView message ;
    EditText broadcastMessage;


    //zego的音视频引擎以及相应所需要的对象
    ZegoExpressEngine engine;
    ZegoUser user;
    IZegoEventHandler eventHandler;
    //相应的数据匹配
    String appSign;
    Long appID;
    String roomID;
    String userID;
    String streamId;



    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.room_message);
        configEngine();
        loginRoom();
        initUi();
    }

    private void createEventHandler() {
        //构建一个Zego的活动Handler
        eventHandler = new IZegoEventHandler() {
            @Override
                public void onIMRecvBroadcastMessage(String roomID, ArrayList<ZegoBroadcastMessageInfo> messageList) {
                super.onIMRecvBroadcastMessage(roomID, messageList);
                //在这里实现广播信息的展现
                String originalMsg = message.getText().toString();
                String saying = messageList.get(messageList.size()-1).message;
                message.setText(originalMsg+saying);
                Toast.makeText(getApplicationContext(),"saying",Toast.LENGTH_SHORT).show();

            }
        };
    }

    private void loginRoom() {
        ZegoRoomConfig roomConfig = new ZegoRoomConfig();
        roomConfig.isUserStatusNotify = true;//确保每个登陆的用户都是为true才开启广播
        //配置
        engine.enableCamera(true);
        engine.muteMicrophone(false);
        engine.muteSpeaker(false);
        engine.loginRoom(roomID,user,roomConfig);

    }

    private void configEngine() {
        appID = 1609341984L;
        appSign = "c79f866a600627e655ee046b04381f4b459ab530d7dae2cef11495c16dcb45a3";
        roomID = "theRoomId";
        userID = getIntent().getStringExtra("userId");
        engine = ZegoExpressEngine.createEngine(appID,appSign,true, ZegoScenario.GENERAL,getApplication(),null);
        user  = new ZegoUser(userID);
        createEventHandler();
        engine.setEventHandler(eventHandler);
    }

    private void initUi() {
        message = findViewById(R.id.broadMessage);
        send = findViewById(R.id.broadcastSendButton);
        broadcastMessage = findViewById(R.id.editBroadcast);
        send.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.broadcastSendButton:
                //在这里进行发送广播📢
                String saying = userID+"："+broadcastMessage.getText().toString();
                engine.sendBroadcastMessage(roomID, saying, new IZegoIMSendBroadcastMessageCallback() {
                    @Override
                    public void onIMSendBroadcastMessageResult(int i, long l) {
                        if(i == 0){
                            Toast.makeText(getApplicationContext(),saying,Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                break;
        }
    }
    @Override
    protected void onDestroy() {
        //log out the room
        engine.logoutRoom(roomID);
        //destroy the engine
        ZegoExpressEngine.destroyEngine(null);
        super.onDestroy();
    }
}
